import {Component, OnInit} from '@angular/core';
import {MesTemplate, MesTemplateCondition} from '../../mes-template.type';
import {ActivatedRoute, Router} from '@angular/router';
import {Services} from '../../../../../frame/module/services';
import {ErrRes} from '../../../../../frame/global/err-res';
import {isNullOrUndefined} from 'util';
import {UrlRou} from '../../../../../frame';
import {ModifyMesTemplateComponent} from '../modify-mes-template.component';
import {Zero3Enum} from '../../../../../frame/enum/zero3-enum';
import * as fa from '@fortawesome/free-solid-svg-icons';
import {MesTypeCondition} from '../../../mes-type/mes-type.type';
import {FroalaEditor} from '../../../../../frame/global/froala-editor';

@Component({
    selector: 'app-main-modify-mes-template',
    templateUrl: './main-modify-mes-template.component.html',
    styleUrls: ['./main-modify-mes-template.component.css']
})
export class MainModifyMesTemplateComponent implements OnInit {

    froalaEditorOptions = Object.assign(FroalaEditor.options, {});

    errRes = new ErrRes();

    domain: MesTemplate = new MesTemplate();

    extend = {
        domain: {
            serAudit: {
                nickname: ''
            },
            mesType: {
                items: [],
                icon: fa.faSearchPlus,
                nickname: '消息类型',
                change: (event) => {
                    console.log(event);
                }
            },
        },
    };

    func = {
        loadDomain: (uuid: string) => {
            const condition: MesTemplateCondition = new MesTemplateCondition();
            condition.uuid = uuid;
            Services.mesTemplate.info(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    this.domain = item.domain;
                    this.extend.domain.serAudit.nickname = Zero3Enum.AUDIT_FLAG.get(this.domain.serAudit);
                }
            });
        },
        update: {
            req: (event) => {
                Services.mesTemplate.update(this.domain, this.func.update.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    this.router.navigate([UrlRou.mesTemplate.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public modifyMesTemplate: ModifyMesTemplateComponent, private router: Router, private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.modifyMesTemplate.main = this;
        const uuid = this.route.snapshot.paramMap.get('uuid');
        this.func.loadDomain(uuid);
        //
        Services.mesType.list(new MesTypeCondition(), {
            page: 0,
            size: 999,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(item => {
            console.log(item);
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.extend.domain.mesType.items = [];
                item.page.content.forEach(it => {
                    this.extend.domain.mesType.items.push({id: it.uuid, name: it.serName});
                });
            }
        });
    }

}
